<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>转账 - 银行客户系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        body {
            background-color: #f5f7fa;
            color: #333;
            overflow-x: hidden;
        }

        .app-container {
            display: flex;
            min-height: 100vh;
            width: 100%;
        }

        .sidebar {
            width: 250px;
            background: linear-gradient(to bottom, #1a3a6c, #2c5a8d);
            color: #fff;
            padding: 30px 0;
            flex-shrink: 0;
        }

        .logo-container {
            padding: 0 25px 25px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .logo-icon {
            width: 50px;
            height: 50px;
            background-color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: #1a3a6c;
            font-weight: bold;
        }

        .logo-text {
            font-size: 20px;
            font-weight: 600;
        }

        .user-info {
            display: flex;
            align-items: center;
            padding: 20px 25px;
            gap: 15px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .avatar {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: #ffcc00;
            color: #1a3a6c;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 20px;
        }

        .user-details {
            flex: 1;
        }

        .user-name {
            font-weight: 600;
            margin-bottom: 5px;
            font-size: 16px;
        }

        .user-account {
            font-size: 13px;
            opacity: 0.8;
        }

        .sidebar-menu {
            padding: 0 15px;
        }

        .menu-item {
            padding: 14px 20px;
            display: flex;
            align-items: center;
            gap: 15px;
            cursor: pointer;
            transition: all 0.2s;
            border-radius: 8px;
            margin-bottom: 8px;
        }

        .menu-item:hover {
            background: rgba(255, 255, 255, 0.1);
        }

        .menu-item.active {
            background: rgba(255, 255, 255, 0.15);
            border-left: 4px solid #ffcc00;
        }

        .menu-item i {
            font-size: 18px;
            width: 24px;
            text-align: center;
        }

        .menu-text {
            font-size: 15px;
        }

        .main-content {
            flex: 1;
            display: flex;
            flex-direction: column;
            overflow: auto;
        }

        .topbar {
            background-color: white;
            padding: 0 30px;
            height: 80px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            border-bottom: 1px solid #eee;
            flex-shrink: 0;
        }

        .page-title {
            font-size: 24px;
            font-weight: 600;
            color: #1a3a6c;
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .page-title i {
            background-color: #1a3a6c;
            color: white;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 18px;
        }

        .current-time {
            font-size: 14px;
            color: #666;
            font-weight: 500;
        }

        .content-area {
            flex: 1;
            padding: 30px;
            overflow-y: auto;
            background-color: #f8fafc;
            display: flex;
            justify-content: center;
            align-items: flex-start;
        }

        .transfer-container {
            display: flex;
            justify-content: center;
            width: 100%;
            max-width: 800px;
        }

        .transfer-form-section {
            width: 100%;
            margin-top: 20px;
        }

        .section-title {
            font-size: 20px;
            color: #1a3a6c;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #1a3a6c;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .section-title i {
            color: #1a3a6c;
        }

        .transfer-form {
            background: white;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            padding: 30px;
        }

        .form-group {
            margin-bottom: 25px;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 10px;
            font-weight: 600;
            color: #444;
            font-size: 16px;
        }

        .form-group select,
        .form-group input {
            width: 100%;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .form-group select:focus,
        .form-group input:focus {
            border-color: #1a3a6c;
            box-shadow: 0 0 0 3px rgba(26, 58, 108, 0.1);
            outline: none;
        }

        .account-display {
            padding: 15px;
            background-color: #f8f9fa;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            color: #1a3a6c;
        }

        .account-display .account-number {
            font-weight: 600;
            letter-spacing: 1px;
        }

        .amount-input {
            position: relative;
        }

        .currency-symbol {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 24px;
            font-weight: 600;
            color: #1a3a6c;
        }

        .amount-input input {
            padding-left: 45px;
            font-size: 24px;
            font-weight: 600;
            height: 70px;
        }

        .quick-amounts {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 15px;
            margin-top: 15px;
        }

        .amount-btn {
            background-color: #f0f4f9;
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 12px;
            text-align: center;
            font-weight: 600;
            color: #444;
            cursor: pointer;
            transition: all 0.2s;
        }

        .amount-btn:hover {
            background-color: #e0e8f0;
            border-color: #1a3a6c;
            color: #1a3a6c;
        }

        .btn-group {
            display: flex;
            gap: 15px;
            margin-top: 30px;
        }

        .btn {
            flex: 1;
            padding: 16px;
            border-radius: 8px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            border: none;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .btn-primary {
            background-color: #1a3a6c;
            color: white;
        }

        .btn-primary:hover {
            background-color: #15315a;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(26, 58, 108, 0.3);
        }

        .btn-outline {
            background-color: transparent;
            border: 2px solid #1a3a6c;
            color: #1a3a6c;
        }

        .btn-outline:hover {
            background-color: #f0f4f9;
        }


        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1000;
            align-items: center;
            justify-content: center;
        }

        .modal-content {
            background-color: white;
            border-radius: 12px;
            box-shadow: 0 5px 25px rgba(0, 0, 0, 0.2);
            width: 100%;
            max-width: 500px;
            padding: 30px;
            text-align: center;
        }

        .modal-icon {
            font-size: 48px;
            margin-bottom: 20px;
        }

        .success-icon {
            color: #2ecc71;
        }

        .error-icon {
            color: #e74c3c;
        }

        .modal-title {
            font-size: 24px;
            font-weight: 600;
            margin-bottom: 15px;
            color: #1a3a6c;
        }

        .modal-message {
            font-size: 18px;
            margin-bottom: 25px;
            line-height: 1.6;
        }

        .modal-buttons {
            display: flex;
            gap: 15px;
            justify-content: center;
        }

        .modal-btn {
            padding: 12px 30px;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            border: none;
            transition: all 0.3s ease;
        }

        .modal-btn-primary {
            background-color: #1a3a6c;
            color: white;
        }

        .modal-btn-primary:hover {
            background-color: #15315a;
        }

        .modal-btn-outline {
            background-color: transparent;
            border: 2px solid #1a3a6c;
            color: #1a3a6c;
        }

        .modal-btn-outline:hover {
            background-color: #f0f4f9;
        }

        .confirm-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }

        .confirm-label {
            font-weight: 600;
            color: #555;
        }

        .confirm-value {
            font-weight: 500;
            color: #333;
            text-align: right;
        }

        .transfer-confirm-info {
            margin: 20px 0;
            padding: 20px;
            background-color: #f9f9f9;
            border-radius: 8px;
        }

        /* 响应式设计 */
        @media (max-width: 900px) {
            .app-container {
                flex-direction: column;
            }

            .sidebar {
                width: 100%;
            }
        }

        @media (max-width: 576px) {
            .topbar {
                padding: 0 20px;
                height: 70px;
                flex-direction: column;
                justify-content: center;
                gap: 5px;
            }

            .page-title {
                font-size: 20px;
            }

            .content-area {
                padding: 20px;
            }

            .quick-amounts {
                grid-template-columns: repeat(2, 1fr);
            }
        }
    </style>
</head>
<body>
    <div class="app-container">
        <!-- 左侧菜单 -->
        <div class="sidebar">
            <div class="logo-container">
                <div class="logo">
                    <div class="logo-icon">
                        <i class="fas fa-university"></i>
                    </div>
                    <div class="logo-text">银行系统</div>
                </div>
            </div>

            <div class="user-info">
                <div class="avatar" id="user-avatar"></div>
                <div class="user-details">
                    <div class="user-name" id="user-name"></div>
                    <div class="user-account" id="user-account"></div>
                </div>
            </div>

            <div class="sidebar-menu">
                <div class="menu-item" onclick="location.href='../user_home.html';">
                    <i class="fas fa-wallet"></i>
                    <div class="menu-text">账户信息</div>
                </div>
                <div class="menu-item" onclick="location.href='Deposit.html';">
                    <i class="fas fa-money-bill-wave"></i>
                    <div class="menu-text">存款</div>
                </div>
                <div class="menu-item" onclick="location.href='Withdraw money.html';">
                    <i class="fas fa-hand-holding-usd"></i>
                    <div class="menu-text">取款</div>
                </div>
                <div class="menu-item active">
                    <i class="fas fa-exchange-alt"></i>
                    <div class="menu-text">转账</div>
                </div>
                <div class="menu-item" onclick="location.href='AccountLost.html';">
                    <i class="fas fa-lock"></i>
                    <div class="menu-text">账户挂失</div>
                </div>
                <div class="menu-item" onclick="location.href='ChangePassword.html';">
                    <i class="fas fa-key"></i>
                    <div class="menu-text">修改密码</div>
                </div>
                <div class="menu-item" onclick="location.href='TransactionRecords.html';">
                    <i class="fas fa-history"></i>
                    <div class="menu-text">交易记录</div>
                </div>
                <div class="menu-item" id="logout-btn">
                    <i class="fas fa-sign-out-alt"></i>
                    <div class="menu-text">安全退出</div>
                </div>
            </div>
        </div>

        <!-- 右侧内容区 -->
        <div class="main-content">
            <div class="topbar">
                <div class="page-title">
                    <i class="fas fa-exchange-alt"></i>
                    <span>转账服务</span>
                </div>
                <div class="current-time" id="current-time"></div>
            </div>

            <div class="content-area">
                <div class="transfer-container">
                    <div class="transfer-form-section">
                        <h2 class="section-title">
                            <i class="fas fa-paper-plane"></i>转账操作
                        </h2>

                        <div class="transfer-form">
                            <div class="form-group">
                                <label>转出账户</label>
                                <div class="account-display">
                                    <i class="fas fa-credit-card"></i>
                                    <span class="account-number" id="from-account">
                                    </span>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="to-account">收款账号</label>
                                <input type="text" id="to-account" placeholder="请输入收款人账号">
                            </div>

                            <div class="form-group">
                                <label for="recipient-name">收款人姓名</label>
                                <input type="text" id="recipient-name" placeholder="请输入收款人真实姓名">
                            </div>

                            <div class="form-group">
                                <label for="amount">转账金额 (¥)</label>
                                <div class="amount-input">
                                    <span class="currency-symbol">¥</span>
                                    <input type="number" id="amount" placeholder="请输入转账金额" min="0" step="100">
                                </div>

                                <div class="quick-amounts">
                                    <div class="amount-btn" data-amount="100">¥100</div>
                                    <div class="amount-btn" data-amount="500">¥500</div>
                                    <div class="amount-btn" data-amount="1000">¥1,000</div>
                                    <div class="amount-btn" data-amount="2000">¥2,000</div>
                                    <div class="amount-btn" data-amount="5000">¥5,000</div>
                                    <div class="amount-btn" data-amount="10000">¥10,000</div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="remark">转账备注 (可选)</label>
                                <input type="text" id="remark" placeholder="例如：还款、货款等">
                            </div>

                            <div class="btn-group">
                                <button class="btn btn-outline" id="btn-cancel">
                                    <i class="fas fa-times"></i> 取消
                                </button>
                                <button class="btn btn-primary" id="btn-transfer">
                                    <i class="fas fa-paper-plane"></i> 确认转账
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 转账确认模态框 -->
    <div class="modal" id="transfer-confirm-modal">
        <div class="modal-content">
            <div class="modal-icon" style="color: #1a3a6c;">
                <i class="fas fa-file-invoice-dollar fa-2x"></i>
            </div>
            <h2 class="modal-title">转账确认</h2>

            <div class="transfer-confirm-info">
                <div class="confirm-row">
                    <div class="confirm-label">转出账户:</div>
                    <div class="confirm-value" id="confirm-from"></div>
                </div>
                <div class="confirm-row">
                    <div class="confirm-label">收款账户:</div>
                    <div class="confirm-value" id="confirm-to"></div>
                </div>
                <div class="confirm-row">
                    <div class="confirm-label">转账金额:</div>
                    <div class="confirm-value" id="confirm-amount"></div>
                </div>
                <div class="confirm-row">
                    <div class="confirm-label">转账备注:</div>
                    <div class="confirm-value" id="confirm-remark"></div>
                </div>
            </div>

            <div class="modal-buttons">
                <button class="modal-btn modal-btn-outline" id="btn-edit-transfer">
                    <i class="fas fa-edit"></i> 修改
                </button>
                <button class="modal-btn modal-btn-primary" id="btn-confirm-transfer">
                    <i class="fas fa-check-circle"></i> 确认转账
                </button>
            </div>
        </div>
    </div>

    <!-- 成功模态框 -->
    <div class="modal" id="success-modal">
        <div class="modal-content">
            <div class="modal-icon success-icon">
                <i class="fas fa-check-circle"></i>
            </div>
            <h2 class="modal-title">转账成功</h2>
            <p class="modal-message" id="success-message">
                您的转账操作已成功完成！
            </p>
            <div class="modal-buttons">
                <button class="modal-btn modal-btn-primary" id="btn-success-ok">确定</button>
                <button class="modal-btn modal-btn-outline" id="btn-print-receipt">打印凭条</button>
            </div>
        </div>
    </div>

    <!-- 错误模态框 -->
    <div class="modal" id="error-modal">
        <div class="modal-content">
            <div class="modal-icon error-icon">
                <i class="fas fa-exclamation-circle"></i>
            </div>
            <h2 class="modal-title">转账失败</h2>
            <p class="modal-message" id="error-message">
                转账过程中出现错误，请稍后重试。
            </p>
            <div class="modal-buttons">
                <button class="modal-btn modal-btn-primary" id="btn-error-ok">确定</button>
            </div>
        </div>
    </div>
<script>
        // 获取用户信息
        function getUserInfo() {
            const cardID = sessionStorage.getItem('cardID');
            const userName = sessionStorage.getItem('userName');
            if (!cardID) {
                alert('请先登录系统');
                window.location.href = '../login.html';
                return null;
            }
            return { cardID, userName };
        }

        // 初始化用户信息
        function initUserInfo() {
            const userInfo = getUserInfo();
            if (!userInfo) return;

            const formattedCardID = userInfo.cardID.replace(/(\d{4})(?=\d)/g, '$1 ');
            document.getElementById('user-name').textContent = userInfo.userName;
            document.getElementById('user-account').textContent = `账号: ${formattedCardID}`;
            document.getElementById('user-avatar').textContent = userInfo.userName.charAt(0);
            document.getElementById('from-account').textContent = formattedCardID;
        }

        // 更新时间显示
        function updateTime() {
            const now = new Date();
            const options = {
                year: 'numeric',
                month: 'long',
                day: 'numeric',
                hour: '2-digit',
                minute: '2-digit',
                second: '2-digit',
                hour12: false
            };
            document.getElementById('current-time').textContent = now.toLocaleDateString('zh-CN', options);
        }

        // 页面加载时初始化
        document.addEventListener('DOMContentLoaded', function() {
            // 初始化用户信息
            initUserInfo();

            // 初始更新时间
            updateTime();

            // 每秒更新一次时间
            setInterval(updateTime, 1000);
        });

        // 收款账号输入验证 - 现在只做简单的显示，不再模拟验证
        document.getElementById('to-account').addEventListener('blur', function() {
            const account = this.value;
            // 如果账号长度大于等于12，则显示收款人信息（这里假设所有账号都是有效的，实际应由后端验证）
            if (account.length >= 12) {
                document.getElementById('recipient-info').style.display = 'block';
            } else {
                document.getElementById('recipient-info').style.display = 'none';
            }
        });

        // 金额输入验证
        document.getElementById('amount').addEventListener('input', function() {
            const amount = parseFloat(this.value) || 0;
            const fee = amount > 50000 ? (amount * 0.0005).toFixed(2) : '0.00';
            const total = amount + parseFloat(fee);
        });

        // 快速金额按钮
        document.querySelectorAll('.amount-btn').forEach(button => {
            button.addEventListener('click', function() {
                const amount = this.getAttribute('data-amount');
                document.getElementById('amount').value = amount;
                // 触发input事件以更新金额显示
                document.getElementById('amount').dispatchEvent(new Event('input'));

                // 添加视觉反馈
                this.style.backgroundColor = '#1a3a6c';
                this.style.color = 'white';

                // 1秒后恢复原样
                setTimeout(() => {
                    this.style.backgroundColor = '';
                    this.style.color = '';
                }, 1000);
            });
        });

        // 转账按钮事件
        document.getElementById('btn-transfer').addEventListener('click', function() {
            const amount = parseFloat(document.getElementById('amount').value);
            const toAccount = document.getElementById('to-account').value;
            const recipient = document.getElementById('recipient-name').value;
            const remark = document.getElementById('remark').value;

            // 验证金额
            if (!amount || amount <= 0) {
                alert('请输入有效的转账金额');
                return;
            }

            // 验证收款信息
            if (!toAccount || toAccount.length < 12) {
                alert('请输入有效的收款账号');
                return;
            }

            if (!recipient) {
                alert('请输入收款人姓名');
                return;
            }

            // 获取当前用户信息
            const userInfo = getUserInfo();
            if (!userInfo) return;

            // 格式化卡号显示
            const formattedFromAccount = userInfo.cardID.replace(/(\d{4})(?=\d)/g, '$1 ');

            // 显示转账确认模态框
            document.getElementById('confirm-from').textContent = `${formattedFromAccount}`;
            document.getElementById('confirm-to').textContent = `${recipient} (${toAccount})`;
            document.getElementById('confirm-amount').textContent = `¥${amount.toLocaleString('zh-CN')}`;
            document.getElementById('confirm-remark').textContent = remark || '无';

            document.getElementById('transfer-confirm-modal').style.display = 'flex';
        });

        // 确认转账按钮事件
        document.getElementById('btn-confirm-transfer').addEventListener('click', async function() {
            // 隐藏确认模态框
            document.getElementById('transfer-confirm-modal').style.display = 'none';

            // 获取转账数据
            const userInfo = getUserInfo();
            if (!userInfo) return;

            const cardID = userInfo.cardID;
            const targetCardID = document.getElementById('to-account').value.replace(/\s+/g, ''); // 移除空格
            const amount = parseFloat(document.getElementById('amount').value);

            // 显示加载状态
            const originalText = this.innerHTML;
            this.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 处理中...';
            this.disabled = true;

            try {
                // 发送转账请求到后端
                const response = await fetch('http://localhost:8080/customer/moveMoney', {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        cardID: cardID,
                        balance: amount,
                        targetCardID: targetCardID
                    })
                });

                const result = await response.json();

                if (response.ok && result.code === '200') {
                    // 转账成功
                    const recipient = document.getElementById('recipient-name').value;
                    document.getElementById('success-message').innerHTML =
                        `您已成功向<span style="color:#1a3a6c;font-weight:bold">${recipient}</span>转账<br>
                        <span style="font-size:24px;color:#e74c3c;font-weight:bold">¥${amount.toLocaleString('zh-CN')}</span>`;

                    // 显示成功模态框
                    document.getElementById('success-modal').style.display = 'flex';

                    // 清空表单
                    document.getElementById('to-account').value = '';
                    document.getElementById('recipient-name').value = '';
                    document.getElementById('amount').value = '';
                    document.getElementById('remark').value = '';
                    document.getElementById('recipient-info').style.display = 'none';
                } else {
                    // 处理错误情况
                    let errorMessage = result.message || '服务器错误';
                    alert(`转账失败: ${errorMessage}`);
                }
            } catch (error) {
                console.error('转账请求出错:', error);
            } finally {
                // 恢复按钮状态
                this.innerHTML = originalText;
                this.disabled = false;
            }
        });

        // 修改转账信息按钮
        document.getElementById('btn-edit-transfer').addEventListener('click', function() {
            document.getElementById('transfer-confirm-modal').style.display = 'none';
        });

        // 成功模态框确定按钮
        document.getElementById('btn-success-ok').addEventListener('click', function() {
            document.getElementById('success-modal').style.display = 'none';
        });

        // 打印凭条按钮
        document.getElementById('btn-print-receipt').addEventListener('click', function() {
            alert('转账凭条已发送至打印机');
            document.getElementById('success-modal').style.display = 'none';
        });

        // 取消按钮事件
        document.getElementById('btn-cancel').addEventListener('click', function() {
            if(confirm('确定要取消转账操作吗？')) {
                // 清空表单
                document.getElementById('to-account').value = '';
                document.getElementById('recipient-name').value = '';
                document.getElementById('amount').value = '';
                document.getElementById('remark').value = '';
                document.getElementById('recipient-info').style.display = 'none';
                // 如果有手续费显示，也重置
                // document.getElementById('fee-value').textContent = '¥0.00';
                // document.getElementById('total-amount').textContent = '¥0.00';
            }
        });

        // 大额转账提示
        document.getElementById('amount').addEventListener('blur', function() {
            const amount = parseFloat(this.value);
            if (amount > 50000) {
                alert('大额转账已开启，请确保收款信息正确');
            }
        });

        // 退出登录
        document.getElementById('logout-btn').addEventListener('click', function() {
            if(confirm('确定要退出系统吗？')) {
                sessionStorage.removeItem('cardID');
                sessionStorage.removeItem('userName');
                window.location.href = '../login.html';
            }
        });
    </script>
</body>
</html>